/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package org.letmeshare.stockkeeper.sale.entities;

import java.io.Serializable;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import org.letmeshare.stockkeeper.stock.entities.Item;
import org.letmeshare.stockkeeper.validator.ValidationException;
import org.letmeshare.stockkeeper.validator.Validator;

/**
 *
 * @author ibisuser1
 */
@Entity
public class BillEntry implements Serializable, Validator {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private String id;
    @ManyToOne
    private Item item;
    private double qty;
    private boolean excludeExclTax;
    private double billedRate, exclTax;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public Item getItem() {
        return item;
    }

    public void setItem(Item item) {
        this.item = item;
    }

    public double getQty() {
        return qty;
    }

    public void setBilledRate(double billedRate) {
        this.billedRate = billedRate;
    }

    public void setExclTax(double exclTax) {
        this.exclTax = exclTax;
    }

    public void setQty(double qty) {
        this.qty = qty;
    }

    public boolean isExcludeExclTax() {
        return excludeExclTax;
    }

    public void setExcludeExclTax(boolean excludeExclTax) {
        this.excludeExclTax = excludeExclTax;
    }

    public double getBilledRate() {
        return billedRate;
    }

    public double getExclTax() {
        return exclTax;
    }

    public double getAmount() {
        double rate = this.billedRate;
        if (!excludeExclTax) {
            rate += this.billedRate * this.exclTax/100;
        }
        return Math.round(qty* rate * 100.0)/100.0;
    }
    
    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof BillEntry)) {
            return false;
        }
        BillEntry other = (BillEntry) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "org.letmeshare.stockkeeper.entities.sale.BillEntry[ id=" + id + " ]";
    }

    public void validate() throws ValidationException {
        if (item == null) {
            throw new ValidationException("Item required");
        }
        item.validate();
        if (qty <= 0) {
            throw new ValidationException("Invalid quantity");
        }
    }
    
}
